package com.cyj.springbootservice.controller.sms;

import com.cyj.springbootservice.service.impl.SmsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @description:
 * @Author C_Y_J
 * @create 2021-03-22 10:34
 **/
@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private SmsServiceImpl smsService;

    /**
     * 通过手机号码登录 发送验证码
     *
     * @param phone
     * @return
     */
    @GetMapping("/login")
    public ResponseEntity<Object> sendCheckCode(String phone) {

        Map<String, Object> sendCheckCode = this.smsService.sendCheckCode(phone);
        int code = ((Integer) (sendCheckCode.get("code"))).intValue();
        if (3 == code) {
            // 发送成功
            return ResponseEntity.ok(null);
        } else if (code == 1) {
            // 发送失败，上一次发送的验证码还未失效
            String msg = sendCheckCode.get("msg").toString();
            return ResponseEntity.status(HttpStatus.OK).body(msg);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器异常。短信验证码发送失败");
    }

    /**
     * 通过手机号码登录 校验验证码
     *
     * @param phone
     * @param verificationCode
     * @return
     */
    @GetMapping("checkVerificationCode")
    public ResponseEntity<Object> checkVerificationCode(String phone, String verificationCode) {
        try {
            //拿到手机号码和验证码
            Boolean aBoolean = this.smsService.checkVerificationCode(phone, verificationCode);
            //根据返回的布尔值来进行相应的逻辑，这里省略！
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
